package com.ftcs.controller;

import com.ftcs.entity.Permission;
import com.ftcs.entity.Role;
import com.ftcs.entity.User;
import com.ftcs.entity.UserRole;
import com.ftcs.model.PageData;
import com.ftcs.model.UserDTO;
import com.ftcs.service.SystemService;
import cn.hutool.core.collection.CollUtil;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Api(value = "系统管理接口")
@Controller
public class SystemController {
    @Autowired
    private SystemService systemService;

    @GetMapping(value = "/useradmin")
    public String useradmin() {
        return "system/useradmin";
    }

    @GetMapping(value = "/roleadmin")
    public String roleadmin() {
        return "system/roleadmin";
    }

    @GetMapping(value = "/permissionadmin")
    public String permissionadmin() {
        return "system/permissionadmin";
    }

    @GetMapping(value = "/userlist")
    @ResponseBody
    public PageData<UserDTO> userlist(@RequestParam("current") int current, @RequestParam("rowCount") int pageSize) {
        int total = systemService.getAllUsers().size();
        List<User> userlist = systemService.getPageUsers(current, pageSize);

        List<UserDTO> userDTOS = userlist.stream().map(user -> {
            UserDTO userDTO = UserDTO.builder().id(user.getUid()).age(user.getAge()).password(user.getPassword()).tel(user.getTel()).username(user.getUsername()).build();
            List<UserRole> userRoleList = systemService.listRolesByUserid(user.getUid());
            if (CollUtil.isNotEmpty(userRoleList)) {
                List<String> collect = userRoleList.stream().map(userRole -> {
                    Role r = systemService.getRoleById(userRole.getRoleid());
                    return r.getRolename();
                }).collect(Collectors.toList());
                String rolename = CollUtil.join(collect, ",");
                userDTO.setRolelist(rolename);
            }
            return userDTO;
        }).collect(Collectors.toList());

        PageData<UserDTO> pageData = new PageData<>();
        pageData.setCurrent(current);
        pageData.setPageSize(pageSize);
        pageData.setRows(userDTOS);
        pageData.setTotal(total);
        return pageData;
    }

    @GetMapping(value = "/user/{uid}")
    @ResponseBody
    public User getuserinfo(@PathVariable("uid") int userId) {
        return systemService.getUserById(userId);
    }

    @GetMapping(value = "/rolelist")
    @ResponseBody
    public List<Role> rolelist() {
        return systemService.getRoles();
    }

    @GetMapping(value = "/roles")
    @ResponseBody
    public PageData<Role> getallroles(@RequestParam("current") int current, @RequestParam("rowCount") int pageSize) {
        List<Role> roles = systemService.getRoleinfo();
        List<Role> rows = systemService.getpageRoleinfo(current, pageSize);
        PageData<Role> grid = new PageData<Role>();
        grid.setCurrent(current);
        grid.setPageSize(pageSize);
        grid.setTotal(roles.size());
        grid.setRows(rows);
        return grid;
    }

    @GetMapping(value = "/deleteuser/{uid}")
    public String deleteuser(@PathVariable("uid") int uid) {
        systemService.deleteUser(uid);
        return "system/useradmin";
    }

    @GetMapping(value = "/adduser")
    public String adduser(@ModelAttribute("user") User user, @RequestParam(value = "rolename[]", required = false) String[] rolename) {
        if (rolename == null) {
            systemService.addUser(user);
        } else {
            systemService.addUser(user, rolename);
        }
        return "system/useradmin";
    }

    @PostMapping(value = "/updateuser/{uid}")
    public String updateuser(@PathVariable("uid") int uid, @ModelAttribute("user") User user, @RequestParam(value = "rolename[]", required = false) String[] rolename) {
        systemService.updateUser(uid, user, rolename);
        return "system/useradmin";
    }


    @GetMapping(value = "permissionlist")
    @ResponseBody
    public List<Permission> getPermisions() {
        return systemService.getPermisions();
    }

    @GetMapping(value = "/addrole")
    public String addrole(@RequestParam("rolename") String rolename, @RequestParam(value = "permissionname[]") String[] permissionname) {
        Role r = new Role();
        r.setRolename(rolename);
        systemService.addrole(r, permissionname);
        return "system/roleadmin";
    }

    @GetMapping(value = "/deleterole/{rid}")
    public String deleterole(@PathVariable("rid") int rid) {
        systemService.deleteRole(rid);
        return "system/roleadmin";
    }

    @GetMapping(value = "roleinfo/{rid}")
    @ResponseBody
    public Role getRolebyrid(@PathVariable("rid") int rid) {
        return systemService.getRoleById(rid);
    }

    @PostMapping(value = "updaterole/{rid}")
    public String updaterole(@PathVariable("rid") int rid, @RequestParam(value = "permissionname[]") String[] permissionnames) {
        systemService.deleteRolePermission(rid);
        systemService.updateRole(rid, permissionnames);
        return "system/roleadmin";
    }


    @GetMapping(value = "permissions")
    @ResponseBody
    public PageData<Permission> getpermissions(@RequestParam("current") int current, @RequestParam("rowCount") int pageSize) {
        List<Permission> permisions = systemService.getPermisions();
        List<Permission> list = systemService.getPagePermisions(current, pageSize);
        PageData<Permission> grid = new PageData<>();
        grid.setCurrent(current);
        grid.setPageSize(pageSize);
        grid.setTotal(permisions.size());
        grid.setRows(list);
        return grid;
    }

    @PostMapping(value = "addpermission")
    public String addpermission(@RequestParam("permissionname") String permissionname) {
        systemService.addPermission(permissionname);
        return "system/permissionadmin";
    }


    @GetMapping(value = "deletepermission/{pid}")
    public String deletepermission(@PathVariable("pid") int pid) {
        systemService.deletepermission(pid);
        return "system/permissionadmin";
    }
}
